home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / getopt / RCS / getopt.c,v < prev    next >
Encoding:
Text File  |  1990-10-29  |  3.3 KB  |  186 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     90.10.29.13.19.34;  author kupfer;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     90.10.29.11.59.45;  author kupfer;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @Program that shell scripts can use to parse their arguments.
  22. @
  23.  
  24.  
  25. 1.2
  26. log
  27. @Take out ATT getopt subroutine.  Make sure results include trailing
  28. newline.
  29. @
  30. text
  31. @/*
  32. **  GETOPT PROGRAM AND LIBRARY ROUTINE
  33. **
  34. **  I wrote main() and AT&T wrote getopt() and we both put our efforts into
  35. **  the public domain via mod.sources.
  36. **    Rich $alz
  37. **    Mirror Systems
  38. **    (mirror!rs, rs@@mirror.TMC.COM)
  39. **    August 10, 1986
  40. */
  41.  
  42. /* $Header$ */
  43.  
  44. #include <stdio.h>
  45.  
  46.  
  47. #ifndef INDEX
  48. #define INDEX index
  49. #endif
  50.  
  51.  
  52. extern char    *INDEX();
  53. extern int     optind;
  54. extern char    *optarg;
  55.  
  56.  
  57. main(ac, av)
  58.     register int     ac;
  59.     register char     *av[];
  60. {
  61.     register char     *flags;
  62.     register int     c;
  63.  
  64.     /* Check usage. */
  65.     if (ac < 2) {
  66.     fprintf(stderr, "usage: %s flag-specification arg-list\n", av[0]);
  67.     exit(2);
  68.     }
  69.  
  70.     /* Play games; remember the flags (first argument), then splice
  71.        them out so it looks like a "standard" command vector. */
  72.     flags = av[1];
  73.     av[1] = av[0];
  74.     av++;
  75.     ac--;
  76.  
  77.     /* Print flags. */
  78.     while ((c = getopt(ac, av, flags)) != EOF) {
  79.     if (c == '?')
  80.         exit(1);
  81.     /* We assume that shells collapse multiple spaces in `` expansion. */
  82.     printf("-%c %s ", c, INDEX(flags, c)[1] == ':' ? optarg : "");
  83.     }
  84.  
  85.     /* End of flags; print rest of options. */
  86.     printf("-- ");
  87.     for (av += optind; *av; av++)
  88.     printf("%s ", *av);
  89.  
  90.     printf("\n");
  91.     exit(0);
  92. }
  93. @
  94.  
  95.  
  96. 1.1
  97. log
  98. @Initial revision
  99. @
  100. text
  101. @d12 2
  102. d59 2
  103. a61 81
  104. }
  105.  
  106. /*
  107. **  This is the public-domain AT&T getopt(3) code.  I added the
  108. **  #ifndef stuff because I include <stdio.h> for the program;
  109. **  getopt, per se, doesn't need it.  I also added the INDEX/index
  110. **  hack (the original used strchr, of course).  And, note that
  111. **  technically the casts in the write(2) calls shouldn't be there.
  112. */
  113.  
  114. #ifndef NULL
  115. #define NULL    0
  116. #endif
  117. #ifndef EOF
  118. #define EOF    (-1)
  119. #endif
  120. #ifndef INDEX
  121. #define INDEX index
  122. #endif
  123.  
  124.  
  125. #define ERR(s, c)    if(opterr){\
  126.     extern int strlen(), write();\
  127.     char errbuf[2];\
  128.     errbuf[0] = c; errbuf[1] = '\n';\
  129.     (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  130.     (void) write(2, s, (unsigned)strlen(s));\
  131.     (void) write(2, errbuf, 2);}
  132.  
  133. extern int strcmp();
  134. extern char *INDEX();
  135.  
  136. int    opterr = 1;
  137. int    optind = 1;
  138. int    optopt;
  139. char    *optarg;
  140.  
  141. int
  142. getopt(argc, argv, opts)
  143. int    argc;
  144. char    **argv, *opts;
  145. {
  146.     static int sp = 1;
  147.     register int c;
  148.     register char *cp;
  149.  
  150.     if(sp == 1)
  151.         if(optind >= argc ||
  152.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  153.             return(EOF);
  154.         else if(strcmp(argv[optind], "--") == NULL) {
  155.             optind++;
  156.             return(EOF);
  157.         }
  158.     optopt = c = argv[optind][sp];
  159.     if(c == ':' || (cp=INDEX(opts, c)) == NULL) {
  160.         ERR(": illegal option -- ", c);
  161.         if(argv[optind][++sp] == '\0') {
  162.             optind++;
  163.             sp = 1;
  164.         }
  165.         return('?');
  166.     }
  167.     if(*++cp == ':') {
  168.         if(argv[optind][sp+1] != '\0')
  169.             optarg = &argv[optind++][sp+1];
  170.         else if(++optind >= argc) {
  171.             ERR(": option requires an argument -- ", c);
  172.             sp = 1;
  173.             return('?');
  174.         } else
  175.             optarg = argv[optind++];
  176.         sp = 1;
  177.     } else {
  178.         if(argv[optind][++sp] == '\0') {
  179.             sp = 1;
  180.             optind++;
  181.         }
  182.         optarg = NULL;
  183.     }
  184.     return(c);
  185. @
  186.